import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getInfo } from '@/api/manager'
import { removeToken } from '@/hooks/cookies'

export const useUserStore = defineStore('user', () => {
    // 用户信息
    const userInfo = ref(null)

    // 侧边栏菜单信息
    const menus = ref([])

    // 权限信息
    const ruleNames = ref([])

    // 获取用户信息
    const getInfoFunction = async () => {
        return await getInfo().then(res2 => {
            userInfo.value = res2
            menus.value = res2.menus
            ruleNames.value = res2.ruleNames
            return res2
        })
    }

    // 退出登录
    const logout = () => {
        removeToken()
        userInfo.value = null
    }

    // 侧边宽度
    const asideWidth = ref('250px')

    // 展开缩起侧边
    const handleAsideWidth = () => {
        asideWidth.value = asideWidth.value === '250px' ? "64px" : "250px"
    }

    return {
        asideWidth,
        userInfo,
        getInfoFunction,
        logout,
        handleAsideWidth,
        menus,
        ruleNames
    }
})